using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;

using Atomic.Libraries;
using Atomic.Thermodynamics.Phonons;

namespace Atomic.Interfaces.Yphon
{
	[Serializable]
	public class YphonPhononDispersion : PhononDispersion
	{
		private RawDataFile dataFile, plotFile, vlineFile;

		public YphonPhononDispersion(IEnumerable<PhononDispersionBranch> bands, RawDataFile dataFile, RawDataFile plotFile, RawDataFile vlineFile)
			: base(bands)
		{
			this.dataFile = dataFile;
			this.plotFile = plotFile;
			this.vlineFile = vlineFile;
		}

		public void YphonPlot()
		{
			using (TempDirectory temp = new TempDirectory())
			{
				Write(temp.Directory);

				Process p = new Process();
				p.StartInfo.FileName = "gnuplot";
				p.StartInfo.Arguments = "vdis.plt";
				p.StartInfo.WorkingDirectory = temp.Directory.FullName;
				p.Start();
				p.WaitForExit();

				if (p.ExitCode != 0)
				{
					throw new Exception("Running gnuplot failed.");
				}
			}
		}

		public void Write(DirectoryInfo directory)
		{
			dataFile.Write(directory.AddFile("vdis.out"));
			plotFile.Write(directory.AddFile("vdis.plt"));
			vlineFile.Write(directory.AddFile("vline.dat"));
		}
	}
}
